package com.woniuxy.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.hospital.mapper.HplDoctorMapper;
import com.woniuxy.hospital.service.IHplDepartmentService;
import com.woniuxy.hospital.service.IHplDoctorService;
import com.woniuxy.hospital.entity.HplDoctor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.hospital.service.IHplTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-08-12
 */
@Service
public class HplDoctorServiceImpl extends ServiceImpl<HplDoctorMapper, HplDoctor> implements IHplDoctorService {

    @Autowired
    private HplDoctorMapper hplDoctorMapper;

    @Autowired
    private IHplDepartmentService iHplDepartmentService;

    @Autowired
    private IHplTitleService iHplTitleService;

    //按条件查询医生
    public List<HplDoctor> getByCondition(HplDoctor hplDoctor){
        //封装查询条件
        QueryWrapper<HplDoctor> queryWrapper = new QueryWrapper<>();
        if(hplDoctor.getName() != null && hplDoctor.getName().trim().length()!=0){
            queryWrapper.like("name",hplDoctor.getName());
        }
        if(hplDoctor.getTitleId() != null && hplDoctor.getTitleId() != 0){
            queryWrapper.eq("title_id",hplDoctor.getTitleId());
        }
        if(hplDoctor.getStartTime() != null && hplDoctor.getStartTime().trim().length()!=0){
            queryWrapper.gt("birth_date",hplDoctor.getStartTime());
        }
        if(hplDoctor.getEndTime() != null && hplDoctor.getEndTime().trim().length()!=0){
            queryWrapper.lt("birth_date",hplDoctor.getEndTime());
        }
        List<HplDoctor> doctors = hplDoctorMapper.selectList(queryWrapper);
        for (HplDoctor doctor : doctors) {
            doctor.setDepName(iHplDepartmentService.getById(doctor.getDepId()).getName());
        }
        for (HplDoctor doctor : doctors) {
            doctor.setTitleName(iHplTitleService.getById(doctor.getTitleId()).getName());
        }
        return doctors;
    }
}
